import Vue from 'vue';

import filter from '../../../../grsc-base/util/filter';
import Loading from '../../../../grsc-base/component/Loading.vue';
import lib from '../../../common/lib';
import WebBox from '../../../component/WebBox/WebBox';
import Trend from './component/Trend.vue'
import lang from './lang.js';
import service from '../service';
import commonService from '../../../common/commonService';
import tpl from './tpl.html';
import mathtool from '../../../../grsc-base/util/mathtool'



export default Vue.extend({
    template: tpl,
    components: {
        WebBox,
        Loading,
        Trend
        // Up,
        // Down
    },
    data() {
        return {
            isLoading: true,
            isCanBack: true,
            isPreBack:false,
            userAccount: '--',
            fundAssetCode: '--',
            fundAssetCodeAmount: '--',
            worth: '--',
            totalWorth: '--',
            perUnit: '--',
            level: '0',
            buyTicket: false,  // 是否已经购买入场券  UNPURCHASED  PURCHASED  RETURNED
            fundBeginDate: null,
            fundEndDate: null,
            ticketStatus: null,
            isTradeOpen: false,
            klineData:null
        }
    },
    created() {
        this._isLogin();
        this._init();
        this._fetchFinanceInfo();
        this._fetchCurrentPrice();

        this.localKlineData();

        // let that = this;
        // setTimeout(()=>{
        //     that.isLoading = false;
        // },1000)
    },
    computed:{
        totalWorth(){
            if($.isNumeric(this.worth) && $.isNumeric(this.fundAssetCodeAmount)){
                return filter.floorFix(mathtool.multiplication(this.worth,this.fundAssetCodeAmount));
            }else {
                return '--'
            }

        }
    },
    mounted() {
        // 判断交易对是否开放
        service.isSymbolTradeOpen({
            'symbolName': 'USDT_SMCF1'
        }).done((rep) => {
            if (rep.code === window.CODE.SUCCESS) {
                if (rep.data && rep.data.status === 'LISTED') {
                    this.isTradeOpen = true;
                } else {
                    this.isTradeOpen = false;
                }
            } else {
                window.error(rep.msg)
            }
        })
    },
    methods: {
        _init(){
            
        },
        _isLogin(){
            if(!lib.isLogin()){
                let fromPath = window.encodeURIComponent(window.location.href + 'profit');
                window.location.href = './login.html?from=' + fromPath;
            }
        },
        _fetchFinanceInfo(){
            this.isLoading = true;
            let param =  {
                fundAssetCode: 'SMCF1'
            };
            let header;
            service.getFinanceInfo(param, header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.userAccount = rep.data.userAccount;
                    this.fundAssetCode = rep.data.fundAssetCode;
                    this.fundAssetCodeAmount = rep.data.fundAssetCodeAmount;
                    // this.worth = rep.data.worth? rep.data.worth : '--';
                    // this.totalWorth = rep.data.totalWorth? rep.data.totalWorth : '--';
                    // this.perUnit = rep.data.perUnit? rep.data.perUnit : '--';
                    this.level = rep.data.level;
                    this.buyTicket = rep.data.buyTicket;
                    this.fundBeginDate = rep.data.fundBeginDate;
                    this.fundEndDate = rep.data.fundEndDate;
                    this.ticketStatus = rep.data.ticketStatus;

                    this.isLoading = false;

                    let date = new Date();
                    let fundEndDate = new Date(this.fundEndDate);
                    //若已购买入场券且当前日期大于可换回时间，可换回按钮可点
                    if((date>=fundEndDate)&&this.buyTicket==='PURCHASED'){
                        this.isCanBack = true;
                    }else{
                        this.isCanBack = false;
                    }

                    if(rep.data.preReturn && this.buyTicket ==='PURCHASED'){
                        this.isPreBack = true;
                    }else{
                        this.isPreBack = false;
                    }
                } else {
                    window.error(rep.msg);
                }
            })
        },

        _fetchCurrentPrice(){
              let param = {
                  symbol:'USDT_SMCF1'
              };
              commonService.loopTradeInfo(param).done(rep=>{
                  if(rep === 'nil'){
                      return;
                  }
                  if (rep['order']) {
                      this.perUnit = filter.floorFixPrice(rep['order'][0].price,2);
                  }
              })
        },

        ticketReturn(){
            let param =  {
                fundAssetCode: 'SMCF1',
            };
            let header;
            service.ticketReturn(param, header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.success(rep.msg);
                    this._init();
                    this._fetchFinanceInfo();
                } else {
                    window.error(rep.msg)
                }
            })
        },

        ticketPreReturn(){
            let param = {
                fundAssetCode: 'SMCF1',
            };
            service.preReturn(param).done(rep =>{
                if(rep.code === window.CODE.SUCCESS){
                    window.success(rep.msg);
                    this._init();
                    this._fetchFinanceInfo();
                }else {
                    window.error(rep.msg)
                }
            })
        },

        localKlineData(){
            let that = this;
            //查看localstorage 存储是否存储最新的 历史数据
            let localhis = window.localStorage.getItem('smcf1his');
            localhis = JSON.parse(localhis);

            let date = new Date();
            let second = date.getTime();

            if(localhis && (second - (localhis[localhis.length-1][0]) <= 7200 * 1000)){
                this.klineData = localhis;
                this.worth = localhis[localhis.length-1][1];
            }else{
                service.getSMCF1History().then((rep) => {

                    let temp = rep;
                    //获取数据后 重新绘制图像
                    let price = [];
                    let len =  temp.length;

                    for(let i = 0;i<len;i++){
                        price.push([
                            temp[i][1]*1000,
                            temp[i][0]
                        ]);
                    }
                    that.klineData = price;
                    that.worth = price[price.length-1][1];

                    window.localStorage.setItem('smcf1his',JSON.stringify(price));

                }).fail((rep) => {

                }).always(()=> {

                });
            }
        }
    }
});